#!/bin/sh
# SPDX-License-Identifier: GPL-2.0
#
# link seminix
#
# seminix is linked from the objects selected by $(KBUILD_SEMINIX_OBJS) and
# $(KBUILD_SEMINIX_LIBS). Most are built-in.a files from top-level directories
# in the kernel tree, others are specified in arch/$(ARCH)/Makefile.
# $(KBUILD_SEMINIX_LIBS) are archives which are linked conditionally
# (not within --whole-archive), and do not require symbol indexes added.
#
# seminix
#   ^
#   |
#   +--< $(KBUILD_SEMINIX_OBJS)
#   |    +--< init/built-in.a drivers/built-in.a mm/built-in.a + more
#   |
#   +--< $(KBUILD_SEMINIX_LIBS)
#   |    +--< lib/lib.a + more
#   |
#   +-< ${kallsymso} (see description in KALLSYMS section)
#
# seminix version (uname -v) cannot be updated during normal
# descending-into-subdirs phase since we do not yet know if we need to
# update seminix.
# Therefore this step is delayed until just before final link of seminix.
#
# System.map is generated to document addresses of all kernel symbols

# Error out on error
set -e

LD="$1"
KBUILD_LDFLAGS="$2"
LDFLAGS_seminix="$3"

is_enabled() {
	grep -q "^$1=y" include/config/auto.conf
}

# Nice output in kbuild format
# Will be supressed by "make -s"
info()
{
	printf "  %-7s %s\n" "${1}" "${2}"
}

# Link of seminix
# ${1} - output file
# ${2}, ${3}, ... - optional extra .o files
seminix_link()
{
	local output=${1}
	local objs
	local libs
	local ld
	local ldflags
	local ldlibs

	info LD ${output}

	# skip output file argument
	shift

	if is_enabled CONFIG_LTO_CLANG || is_enabled CONFIG_X86_KERNEL_IBT; then
		# Use seminix.o instead of performing the slow LTO link again.
		objs=seminix.o
		libs=
	else
		objs="${KBUILD_SEMINIX_OBJS}"
		libs="${KBUILD_SEMINIX_LIBS}"
	fi

	if [ "${SRCARCH}" = "um" ]; then
		wl=-Wl,
		ld="${CC}"
		ldflags="${CFLAGS_seminix}"
		ldlibs="-lutil -lrt -lpthread"
	else
		wl=
		ld="${LD}"
		ldflags="${KBUILD_LDFLAGS} ${LDFLAGS_seminix}"
		ldlibs=
	fi

	ldflags="${ldflags} ${wl}--script=${objtree}/${KBUILD_LDS}"

	# The kallsyms linking does not need debug symbols included.
	if [ "$output" != "${output#.tmp_seminix.kallsyms}" ] ; then
		ldflags="${ldflags} ${wl}--strip-debug"
	fi

	if is_enabled CONFIG_SEMINIX_MAP; then
		ldflags="${ldflags} ${wl}-Map=${output}.map"
	fi

	${ld} ${ldflags} -o ${output}					\
		${wl}--whole-archive ${objs} ${wl}--no-whole-archive	\
		${wl}--start-group ${libs} ${wl}--end-group		\
		$@ ${ldlibs}
}

# Create ${2} .S file with all symbols from the ${1} object file
kallsyms()
{
	local kallsymopt;

	if is_enabled CONFIG_KALLSYMS_ALL; then
		kallsymopt="${kallsymopt} --all-symbols"
	fi

	if is_enabled CONFIG_KALLSYMS_ABSOLUTE_PERCPU; then
		kallsymopt="${kallsymopt} --absolute-percpu"
	fi

	if is_enabled CONFIG_KALLSYMS_BASE_RELATIVE; then
		kallsymopt="${kallsymopt} --base-relative"
	fi

	info KSYMS ${2}
	${NM} -n ${1} | scripts/kallsyms ${kallsymopt} > ${2}
}

# Perform one step in kallsyms generation, including temporary linking of
# seminix.
kallsyms_step()
{
	kallsymso_prev=${kallsymso}
	kallsyms_seminix=.tmp_seminix.kallsyms${1}
	kallsymso=${kallsyms_seminix}.o
	kallsyms_S=${kallsyms_seminix}.S

	seminix_link ${kallsyms_seminix} "${kallsymso_prev}"
	kallsyms ${kallsyms_seminix} ${kallsyms_S}

	info AS ${kallsyms_S}
	${CC} ${NOSTDINC_FLAGS} ${SEMINIXINCLUDE} ${KBUILD_CPPFLAGS} \
	      ${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} \
	      -c -o ${kallsymso} ${kallsyms_S}
}

# Create map file with all symbols from ${1}
# See mksymap for additional details
mksysmap()
{
	${CONFIG_SHELL} "${srctree}/scripts/mksysmap" ${1} ${2}
}

# Delete output files in case of error
cleanup()
{
	rm -f System.map
	rm -f seminix
	rm -f seminix.map
	rm -f .seminix.objs
}

# Use "make V=1" to debug this script
case "${KBUILD_VERBOSE}" in
*1*)
	set -x
	;;
esac

if [ "$1" = "clean" ]; then
	cleanup
	exit 0
fi

# Update version
info GEN .version
if [ -r .version ]; then
	VERSION=$(expr 0$(cat .version) + 1)
	echo $VERSION > .version
else
	rm -f .version
	echo 1 > .version
fi;

# final build of init/
${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init need-builtin=1

#link seminix.o
${MAKE} -f "${srctree}/scripts/Makefile.seminix_o"

# Generate the list of in-tree objects in seminix
#
# This is used to retrieve symbol versions generated by genksyms.
for f in ${KBUILD_SEMINIX_OBJS} ${KBUILD_SEMINIX_LIBS}; do
	case ${f} in
	*libgcc.a)
		# Some architectures do '$(CC) --print-libgcc-file-name' to
		# borrow libgcc.a from the toolchain.
		# There is no EXPORT_SYMBOL in external objects. Ignore this.
		;;
	*.a)
		${AR} t ${f} ;;
	*)
		echo ${f} ;;
	esac
done > .seminix.objs

kallsymso=""
kallsymso_prev=""
kallsyms_seminix=""
if is_enabled CONFIG_KALLSYMS; then

	# kallsyms support
	# Generate section listing all symbols and add it into seminix
	# It's a three step process:
	# 1)  Link .tmp_seminix.kallsyms1 so it has all symbols and sections,
	#     but __kallsyms is empty.
	#     Running kallsyms on that gives us .tmp_kallsyms1.o with
	#     the right size
	# 2)  Link .tmp_seminix.kallsyms2 so it now has a __kallsyms section of
	#     the right size, but due to the added section, some
	#     addresses have shifted.
	#     From here, we generate a correct .tmp_seminix.kallsyms2.o
	# 3)  That link may have expanded the kernel image enough that
	#     more linker branch stubs / trampolines had to be added, which
	#     introduces new names, which further expands kallsyms. Do another
	#     pass if that is the case. In theory it's possible this results
	#     in even more stubs, but unlikely.
	#     KALLSYMS_EXTRA_PASS=1 may also used to debug or work around
	#     other bugs.
	# 4)  The correct ${kallsymso} is linked into the final seminix.
	#
	# a)  Verify that the System.map from seminix matches the map from
	#     ${kallsymso}.

	kallsyms_step 1
	kallsyms_step 2

	# step 3
	size1=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso_prev})
	size2=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso})

	if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
		kallsyms_step 3
	fi
fi

seminix_link seminix "${kallsymso}"

info SYSMAP System.map
mksysmap seminix System.map

# step a (see comment above)
if is_enabled CONFIG_KALLSYMS; then
	mksysmap ${kallsyms_seminix} .tmp_System.map

	if ! cmp -s System.map .tmp_System.map; then
		echo >&2 Inconsistent kallsyms data
		echo >&2 Try "make KALLSYMS_EXTRA_PASS=1" as a workaround
		exit 1
	fi
fi

# For fixdep
echo "seminix: $0" > .seminix.d
